﻿using Genius.Brain.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Genius.Brain.Shared.BO;
using System.IO;
using Newtonsoft.Json.Linq;
using Genius.Brain.Shared.Locator;
using Genius.Brain.Impl.Strategy;
using Genius.Brain.Shared.Strategy;

namespace Genius.Brain.Impl
{
    class JsonDatabase<T> : IJsonDatabase<T>
    {
        private IJsonConverterStrategy<T> _converterStrategy;
        public JsonDatabase(IJsonConverterStrategy<T> converterStrategy)
        {
            _converterStrategy = converterStrategy;
            Data = new List<T>();
        }
        public IList<T> Data
        {
            get; private set;
        }

        public void LoadFile(string fileName)
        {
            var json = File.ReadAllText(fileName);
            var db   = JObject.Parse(json);

            //version auswerten
            var data = db["data"];
            foreach(var rec in data)
            {
                var d = _converterStrategy.ConvertFromJson(rec);
                Data.Add(d);
            }

        }
    }
}
